package serviceimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.qcloud.sign.FileCloudSign;

import lib.JsonHelper;
import service.ws.BareResponseProcessor;
import service.ws.CommonWSHandler;
import service.ws.WSHandler;

public class TencentVideo {

	private static final int appid = 1251122768;
    private static final String bucket = "baihejia";
    private static final String secretId = "AKID3vZy98PMaZImcErmhPs78uSlpdBUJO1R";
    private static final String secretKey = "KRPAiBUaT6TbT4PVLuDG3aqqb8PRDj01";
    private static final String pornDetect = "http://service.image.myqcloud.com/detection/porn_detect";
	
    public static void main(String[] args) throws Exception {
    	List<String> imgUrl = new ArrayList<>();
		imgUrl.add("http://nos.netease.com/yidun/2-0-0-4f903f968e6849d3930ef0f50af74fc2.jpg");
		checkEroticism(imgUrl);
	}
	public static List<String> checkEroticism(List<String> imgUrls) throws Exception{
		List<String> errorImg = new ArrayList<>();
		try {
			int size = imgUrls.size();
			int pointsDataLimit = 20;
			int part = size/pointsDataLimit;
			Map<String, Object> params = new HashMap<>();
	        params.put("appid", appid);
	        params.put("bucket", bucket);
	        params.put("url_list", imgUrls);
	        WSHandler wsHandler = new CommonWSHandler(pornDetect);
	        wsHandler.buildPostBody(JsonHelper.toString(params));
	        wsHandler.addHeader(new BasicHeader("Authorization", getSign()));
			//分批上传
			if(imgUrls.size()>pointsDataLimit){
				for (int i = 0; i < part; i++) {
					List<String> subImgUrls = imgUrls.subList(0, pointsDataLimit);
					params.put("url_list", subImgUrls);
					wsHandler.buildPostBody(JsonHelper.toString(params));
					errorImg.addAll(checkEroticismPoint(wsHandler));
					imgUrls.subList(0, pointsDataLimit).clear();
				}
			}
			if(imgUrls.size()>0){
				params.put("url_list", imgUrls);
				wsHandler.buildPostBody(JsonHelper.toString(params));
				errorImg.addAll(checkEroticismPoint(wsHandler));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return errorImg;
	}
	private static List<String> checkEroticismPoint(WSHandler wsHandler) throws Exception{
        wsHandler.post();
        HttpResponse httpResponse = wsHandler.process(BareResponseProcessor.getInstance());
        String responseString = EntityUtils.toString(httpResponse.getEntity());
        JSONObject json = new JSONObject(responseString);
        JSONArray jsonArray = json.getJSONArray("result_list");
        List<String> errorImg = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
        	JSONObject jsonObject = jsonArray.getJSONObject(i);
        	int code = jsonObject.getInt("code");
        	System.out.println(code);
        	//code:服务器错误码，0为成功,result:供参考的识别结果，0正常，1黄图，2疑似图片
        	if(code==0){
        		JSONObject jsonData = jsonObject.getJSONObject("data");
        		System.out.println(jsonData.getInt("result"));
        		if(code==0&&jsonData.getInt("result")!=0){
            		errorImg.add(jsonObject.getString("url"));
            	}
        	}else{
        		
        	}
		}
        return errorImg;
	}
	/**
	 * 生成签名
	 * @return
	 */
	private static String getSign(){
		long expired = System.currentTimeMillis() / 1000 + 2592000;
		return FileCloudSign.appSignV2(appid, secretId, secretKey, bucket, expired);
	}
}
